//
//  NSArray+PinYin.m
//  dasdada
//
//  Created by wanzhao on 15/7/20.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "NSArray+PinYin.h"
#import "CContactModel.h"
#import "CGroupModel.h"

@implementation NSArray (PinYin)

+(NSMutableArray*)IndexArray:(NSArray*)array
{
    NSArray *sortArray = [self sortArray:array];
    NSMutableArray *A_Result=[NSMutableArray array];
    NSString *tempString;
    
    for (id object in sortArray)
    {
        if ([object isKindOfClass:[CContactModel class]]) {
            CContactModel *contact = (CContactModel *)object;
            if (contact.PinYin.length > 0) {
                NSString *pinyin = [[contact.PinYin substringToIndex:1] uppercaseString];
                //不同
                if(![tempString isEqualToString:pinyin])
                {
                    // NSLog(@"IndexArray----->%@",pinyin);
                    [A_Result addObject:pinyin];
                    tempString = pinyin;
                }
            }

        } else if ([object isKindOfClass:[CGroupModel class]]){
            
            CGroupModel *groupModel = (CGroupModel *)object;
            if (groupModel.PinYin.length > 0) {
                NSString *pinyin = [[groupModel.PinYin substringToIndex:1] uppercaseString];
                //不同
                if(![tempString isEqualToString:pinyin])
                {
                    // NSLog(@"IndexArray----->%@",pinyin);
                    [A_Result addObject:pinyin];
                    tempString = pinyin;
                }
            }

        }
        
    }
    return A_Result;
}

+ (NSArray *)sortArray:(NSArray *)array
{
    return [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"PinYin" ascending:YES]]];
}
+(NSArray *)sortArrayByTime:(NSArray *)array
{
    return [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"GroupCreateDate" ascending:NO]]];

}
#pragma mark - 返回联系人

+(NSMutableArray*)LetterSortArray:(NSArray*)array
{
    NSArray *sortArray = [self sortArray:array];
    NSMutableArray *LetterResult=[NSMutableArray array];
    NSMutableArray *item;
    NSString *tempString ;
    //拼音分组
    for (CContactModel* object in sortArray) {
        if ([object isKindOfClass:[CContactModel class]]){
            CContactModel *contact = (CContactModel *)object;
            NSString *pinyin = @"#";
            if (contact.PinYin.length > 0) {
                pinyin = [[contact.PinYin substringToIndex:1] uppercaseString];
            }
            //不同
            if(![tempString isEqualToString:pinyin])
            {
                //分组
                item = [NSMutableArray array];
                [item  addObject:contact];
                [LetterResult addObject:item];
                //遍历
                tempString = pinyin;
            }else//相同
            {
                [item  addObject:contact];
            }
 
        } else if ([object isKindOfClass:[CGroupModel class]]) {
        CGroupModel *groupModel = (CGroupModel *)object;
            NSString *pinyin = @"#";
            if (groupModel.PinYin.length > 0) {
                pinyin = [[groupModel.PinYin substringToIndex:1] uppercaseString];
            }
            //不同
            if(![tempString isEqualToString:pinyin])
            {
                //分组
                item = [NSMutableArray array];
                [item  addObject:groupModel];
                [LetterResult addObject:item];
                //遍历
                tempString = pinyin;
            }else//相同
            {
                [item  addObject:groupModel];
            }

        }
            
    }
    return LetterResult;
}


@end
